Daniel Calbimonte
Properties

PostgreSQL Tutorial for beginners

April 6, 2023 by

Introduction to the PostgreSQL tutorial

In this article, we will have a tutorial to learn PostgreSQL. According to Statista, PostgreSQL is the 4th most popular database in the world (the other ones are Oracle, SQL Server, and MySQL). That is why we think it is an important DBMS to learn.

This tutorial is oriented toward newbies and contains the following sections:

Requirements

Tutorial to learn PostgreSQL – Create tables using SQL

For this tutorial, I am assuming that you are already connected to pgAdmin. If not, follow the instructions in the requirements.

First, to create a query go to Databases>postgres>Schemas, right-click and select Query Tool.

Open the Query Tool

The following query creates a table named customers.

ID is an identifier and it is also the primary key. The primary key is used to uniquely identify the different records in the table. Ideally, it is an integer number.

Also, we have the varchar which is used to store characters. The first name, last na,me and email columns use this data type for string characters. The limit used is 50 characters for the firstname and lastname, and the email supports 255 characters.

Finally, the birthday column contains dates. It contains the birthday.

For more information about PostgreSQL data types refer to this link:

Tutorial to learn PostgreSQL – Create tables using the pgAdmin

Alternatively, you can create a Table using the pgAdmin. The pgAdmin is like the SSMS. You can administer the database, create, and edit tables. Modify the data using the UI. The following steps will help you to create your table using the pgAdmin:

First, go to the Browser and then go to Databases>postgres>Schemas>public>Tables, right-click and select the Create>Table option.

Create table with pgAdmin

In the General tab, write the table’s name and then go to Columns.

Add a table name

You can use the + icon to add multiple columns. In this example, we are adding the id with the integer data type. Also, we set this column as a primary key.

The firstname and lastname use the character varying data type and the length equal to 50.

Add columns to the table

Finally, press the Save button.

How to insert data in a table

First, in order to insert data in the table, we will use the insert statement. The following sentences will insert 1 row in the customer table previously created.

To insert data, INSERT INTO is used followed by the table name. Also, you use the word values and in parenthesis, you write the numbers without quotes and then the name, lastname, and email using quotes for strings and then the date using the YYYY-MM-DD format.

Secondly, to insert 3 values with the insert sentence, use the following sentences.

To insert multiple rows, you need to write the values in parentheses separated by commas for each row.

Tutorial to learn PostgreSQL – How to edit a table in pgAdmin

To modify the table in pgAdmin follow these steps. First, right-click the table that you want to modify and select Properties…

Open table properties

Go to the Columns tab and you can add columns using the + icon, edit columns using the pencil and delete columns using the basket icon to delete. Finally, press the Save button.

Delete columns

How to modify (alter) a table using SQL

Alternatively, you can use SQL to modify the tables.

The alter statement is used to modify a table. Let’s take a look at some examples.

First, we will add a column named age of type integer to the customer’s table.

Optionally, we can use the IF EXISTS condition to execute the sentence only if the table exists.

Secondly, to drop the column age, use the following sentence:

If you need to verify if the table and the column exist, you can use the following sentences.

For more information about the ALTER sentence, you can read the following link:

How to update the table’s data with pgAdmin

In pgAdmin right-click the table and select View/Edit data. You can edit all rows or work with the first, and last rows, or use filters. If there aren’t so many rows, the all-rows option is OK. If there are many rows in the table, you may need to work with other options.

Edit data

Double-click the cell that you want to modify.

Modify cells

Write the new value for the cell and press OK.

Edit cell's data

There is an option to save changes.

Delete rows

How to delete a row in pgAdmin

In pgAdmin right-click the table and select View/Edit data>All rows.

Edit view's data

Select the row or rows to delete and press the Delete icon and then save the changes.

Save changes

How to update data using SQL

If you are new in this world, you may wonder why on earth should you learn to handle databases using SQL if you can use the pgAdmin. Sometimes you need to automate tasks of integrating your database with other tools and it is necessary to work with SQL.

The following example shows how to replace the value Sophia to Sofia in the column firstname of the table customers.

How to delete data using SQL

Now, we will show how to delete the data. The following example will delete the row of the table customers where the id is equal to 2.

Working with JOIN in Postgres

The joins are used to get information from 2 or more tables. There are several types of JOINS like the INNER JOIN, FULL OUTER JOIN, LEFT OUTER JOIN, and RIGHT OUTER JOIN.

We created an article for you related to this topic.

Tutorial to learn how to create views in PostgreSQL using pgAdmin

A view in Postgres is a virtual table that stores a query of one or more tables. In pgAdmin, go to the Database and look for the Views. Right-click and select Create>View.

Create a view in pgAdmin

In the General tab write a name for the view and go to the Code tab.

Create - View

In the Code tab, write the following query.

Finally, press Save.

Create - View - Code

How to view or edit data in Views using pgAdmin

You can edit or view data in PostgreSQL views in the same way that you do in tables. Right-click the view and select View/Edit Data>All rows.

View/Edit Data

How to modify (alter) a view in PostgreSQL

To modify a view in PostgreSQL, right-click on the view to alter and select the Properties.

Properties

Go to the Code tab and modify the query according to your need and press Save.

myview - Code

How to create a view using SQL

To create a view in Postgres using SQL, you need to use the create sentence. The following example creates a view named v_customers and shows the lastname of the table customers.

How to edit (alter) a view using SQL. Also, you can modify a view using the alter statement.

The following example will modify an existing view named v_customer.

Postgres in Azure

Azure offers a cloud service. This is a Microsoft Service. We created some nice tutorials to work with Azure and Postgres:

Conclusion

Learning Postgres is a straightforward process if you already worked with other DBMS tools. In this tutorial to learn PostgreSQL, we learned to work with pgAdmin which is a tool similar to SSMS, and also, we learned to use SQL which is similar to the SQL used in SQL Server.

Daniel Calbimonte
Latest posts by Daniel Calbimonte (see all)
PostgreSQL

About Daniel Calbimonte

Daniel Calbimonte is a Microsoft Most Valuable Professional, Microsoft Certified Trainer and Microsoft Certified IT Professional for SQL Server. He is an accomplished SSIS author, teacher at IT Academies and has over 13 years of experience working with different databases. He has worked for the government, oil companies, web sites, magazines and universities around the world. Daniel also regularly speaks at SQL Servers conferences and blogs. He writes SQL Server training materials for certification exams. He also helps with translating SQLShack articles to Spanish View all posts by Daniel Calbimonte

168 Views